package scan

import "strings"

// filter
type filter struct {
	excludeFunc func(path string) bool
}

// newFilter 创建一个新的过滤器，基于提供的排除列表
func newFilter(exclude []string, ignoreCase bool) *filter {
	if exclude == nil || len(exclude) == 0 {
		// 如果排除列表为空，返回一个始终返回 false 的过滤器
		return &filter{
			excludeFunc: func(path string) bool {
				return false
			},
		}
	}

	// 创建排除项的映射，只需构建一次
	excludeMap := make(map[string]struct{})
	for _, e := range exclude {
		if ignoreCase {
			excludeMap[strings.ToLower(e)] = struct{}{}
		} else {
			excludeMap[e] = struct{}{}
		}
	}

	var excludeFunc func(path string) bool

	if ignoreCase {
		// 忽略大小写比较
		excludeFunc = func(path string) bool {
			_, ok := excludeMap[strings.ToLower(path)]
			return ok
		}
	} else {
		excludeFunc = func(path string) bool {
			_, ok := excludeMap[path]
			return ok
		}
	}
	return &filter{
		excludeFunc: excludeFunc,
	}

}

// IsExcluded 判断给定路径是否在排除项中
func (f *filter) isExcluded(path string) bool {
	return f.excludeFunc(path)
}
